package com.topgay.interceptor.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import com.topgay.domain.ValueStrings;

/**
 * @author Harry
 * 관리자 페이지에 로그인 체크
 */
public class LoginCheckInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception exception)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("prehandle");
		response.setHeader("cache-control", "no-store, no-cache");
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		HttpSession session = request.getSession();
		Object loginInfoAdmin = session.getAttribute("login_admin_info");
		if(loginInfoAdmin == null) {
//			ModelAndView mv = new ModelAndView("/admin/login.do", "error_msg", ValueStrings.error_msg_login);
			ModelAndView mv = new ModelAndView("/admin/body/login.jsp", "error_msg", ValueStrings.getInstance().getErrorMsgLogin());
			throw new ModelAndViewDefiningException(mv);
		}
		return true;
	}

	
}
